package jinyilw.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//两个参数的通用执行接口集合
public class ActorSets<T, M>
{
	private volatile List<Actor<T, M>> actors;
	private boolean threadSafe;

	public ActorSets()
	{
	}

	public ActorSets(boolean threadSafe)
	{
		this.threadSafe = threadSafe;
	}

	public void add(Actor<T, M> actor)
	{
		if (actors == null)
			createActors();
		if (!actors.contains(actor))
			actors.add(actor);
	}

	public void remove(Actor<T, M> actor)
	{
		if (actors != null)
			actors.remove(actor);
	}

	public void act(T t, M m)
	{
		if (actors != null)
			for (Actor<T, M> actor : actors)
				actor.act(t, m);
	}

	private synchronized void createActors()
	{
		if (actors == null)
		{
			actors = new ArrayList<>();
			if (threadSafe)
				actors = Collections.synchronizedList(actors);
		}
	}
}
